package com.example.warehouse_manager.service.impl;

import com.example.warehouse_manager.entity.Product;
import com.example.warehouse_manager.entity.Result;
import com.example.warehouse_manager.mapper.ProductMapper;
import com.example.warehouse_manager.page.Page;
import com.example.warehouse_manager.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Yang
* @description 针对表【product(商品表)】的数据库操作Service实现
* @createDate 2024-03-22 15:06:14
*/
@Service
public class ProductServiceImpl implements ProductService{

    @Autowired
    private ProductMapper productMapper;

    //上传的图片访问路径的目录路径
    @Value("${file.access-path}")
    private String fileAccessPath;

    //分页查询商品的业务
    @Override
    public Page queryProductPage(Page page, Product product) {
        //查询商品行数
        Integer count = productMapper.findProductRowCount(product);
        //分页查询商品
        List<Product> productList = productMapper.findProductPage(page, product);
        //组装分页信息
        page.setTotalNum(count);
        page.setResultList(productList);
        return page;
    }

    //添加商品
    @Override
    public Result saveProduct(Product product) {
        //查询商品型号是否已存在
        Product productByNum = productMapper.findProductByNum(product.getProductNum());
        if (productByNum != null){
            //商品已存在
            return Result.err(Result.CODE_ERR_BUSINESS,"该型号已存在");
        }

        //处理上传图片的访问路径
        product.setImgs(fileAccessPath+product.getImgs());
        //添加商品
        int i = productMapper.insertProduct(product);
        if (i > 0){
            return Result.ok("商品添加成功！");
        }


        return Result.err(Result.CODE_ERR_BUSINESS,"商品添加失败");
    }

    //根据商品id修改商品上下架状态
    @Override
    public Result updateStateByPid(Product product) {
        int i = productMapper.setStateByPid(product.getProductId(), product.getUpDownState());
        if (i>0){
            return Result.ok("设置成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"设置失败！");
    }

    //根据商品id删除商品
    @Override
    public Result deleteProductByIds(List<Integer> productIdList) {
        int i = productMapper.removeProductByIds(productIdList);
        if (i >0){
            return Result.ok("删除成功");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"删除失败");
    }

    //根据id修改商品
    @Override
    public Result setProductById(Product product) {
        //判断修改后的型号是否已存在
        Product prod = productMapper.findProductByNum(product.getProductNum());
        if (prod != null && !prod.getProductId().equals(product.getProductId())){
            //商品型号修改后的型号以及存在
            return Result.err(Result.CODE_ERR_BUSINESS,"修改后的型号已存在");
        }

        //判断上传的图片是否被修改
        if (!product.getImgs().contains(fileAccessPath)){
            product.setImgs(fileAccessPath+product.getImgs());
        }

        //修改信息
        int i = productMapper.setProductById(product);
        if (i>0){
            return Result.ok("商品修改成功");
        }

        return Result.err(Result.CODE_ERR_BUSINESS,"商品修改失败");
    }
}




